package src.array;

public class no209 {
    //长度最小的子数组，双指针，滑动窗口
    public static void main(String[] args) {
        int num[] = {1,2,3,4,5};
        int target = 15;
        int result = minSubArrayLen(target, num);
        System.out.println(result);

    }

    public static int minSubArrayLen(int target, int[] nums) {
        int left = 0, right, sum = 0;
        int result = Integer.MAX_VALUE;    //此处不能初始化为nums.length，用例｛1，2，3，4，5｝，target=15不通过
        for (right = 0; right < nums.length; right++) {
            sum += nums[right];
            while (sum >= target) {
                result = Math.min(result, right - left + 1);
                sum -= nums[left];
                left++;
            }

        }
        return result == Integer.MAX_VALUE ? 0 : result;

    }
}
